package com.wangfugui.mybatisplus.utils;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

public class EmailWithAttachmentSender {

    public static void main(String[] args) {
        // 邮件服务器配置
//        String host = "smtp.example.com"; // 替换为你的SMTP服务器
        String host = "imap.exmail.qq.com"; // IMAP服务器
        String port = "587"; // 常用端口：25, 465(SSL), 587(TLS)

        // 发件人信息
        String from = "shenghong@qidianqiuxue.com";
//        String username = "sender@example.com"; // 通常是邮箱地址
//        String password = "yourpassword"; // 邮箱密码或授权码
        // 企业微信邮箱配置
        String username = "shenghong@qidianqiuxue.com"; // 邮箱地址
        String password = "Qidian842"; // 密码或授权码

        // 收件人信息
        String to = "824095775@qq.com";
//        String to = "zhangli@righteous.hk;suwind@126.com;liuyitong@righteous.hk";

        // 邮件内容
        String subject = "测试带附件的邮件";
        String text = "这是一封测试邮件，包含附件。";
        
        // 附件路径
        String attachmentPath = "/Users/zhaolele/Documents/mail/FUTU_Daily_Report20250604.xlsx"; // 替换为实际文件路径
        
        // 发送邮件
        sendEmailWithAttachment(host, port, from, username, password, to, subject, text, attachmentPath);
    }

    public static void sendEmailWithAttachment(String host, String port, 
            final String from, final String username, final String password,
            String to, String subject, String text, String attachmentPath) {
        
        // 配置属性
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.auth", "true");
//        props.put("mail.smtp.starttls.enable", "true"); // 使用TLS
        props.put("mail.smtp.ssl.enable", "true");

        // 创建会话
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        
        try {
            // 创建消息
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject(subject);
            
            // 创建消息体部分
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(text);
            
            // 创建多部分消息
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            
            // 添加附件部分
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(attachmentPath);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(source.getName());
            multipart.addBodyPart(messageBodyPart);
            
            // 设置完整消息
            message.setContent(multipart);
            
            // 发送消息
            Transport.send(message);
            
            System.out.println("带附件的邮件已成功发送！");
            
        } catch (MessagingException e) {
            throw new RuntimeException("发送邮件失败", e);
        }
    }
}